package com.spring.cloud.eurekaclientgateway.filter;

import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//通过 GlobalFilter 接口实现全部过滤器 并加入过滤器排序
//@Component
public class TokenGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //判定请求头token 参数是否存在
        boolean flag = !StringUtils.isBlank(
                exchange.getRequest().getHeaders().getFirst("token"));
        if(flag){
            //存在,直接放行路由
            return chain.filter(exchange);
        }
        //获取token参数
        String token = exchange.getRequest()
                .getQueryParams().getFirst("token");
        ServerHttpRequest request = null;
        //token参数不为空,路由时将它放入请求头
        if(!StringUtils.isBlank(token)){
            //构建请求头                     //mutate变异
            request = exchange.getRequest().mutate()
                    .header("token",token).build();
            //构造请求头后执行责任链
            return chain.filter(exchange.mutate().request(request).build());
        }
        //放行
        return chain.filter(exchange);
    }

    //过滤器排序
    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE + 100001;
    }
}
